//
//  MHCategoryMainViewController.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/24.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHCategoryMainViewController.h"
#import "FDSlideBar.h"
#import "FDSlideContentView.h"
#import "MHCategoryDetailViewController.h"
#import "MHComicDetailViewController.h"
#import "MHCategory.h"
#import <AFNetworking.h>
#define kCategoryHeader @"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx?method=catalogs"

@interface MHCategoryMainViewController ()<FDSlideContentViewDataSource>
@property (strong, nonatomic) FDSlideBar *slideBar;
@property (strong, nonatomic) FDSlideContentView *contentView;
@property (nonatomic, strong) NSMutableArray *categoryArray;

@end

@implementation MHCategoryMainViewController

- (void)acquireData
{
    [[AFHTTPRequestOperationManager manager] GET:kCategoryHeader parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSArray *dataArray = responseObject;
        self.categoryArray = [NSMutableArray array];
        NSMutableArray *titleArray = [NSMutableArray array];
        for (NSDictionary *dict in dataArray) {
            MHCategory *category = [MHCategory modelWithDict:dict];
            [titleArray addObject:dict[@"CatalogName"]];
            [self.categoryArray addObject:category];
        }
        self.slideBar.itemsTitle = titleArray;
        [self createContentView];
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"%@", error.localizedDescription);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    FDSlideBar *sliderBar = [[FDSlideBar alloc] initWithFrame:CGRectMake(0, 64, Width, 30)];
    sliderBar.backgroundColor = [UIColor whiteColor];
    sliderBar.itemColor = [UIColor grayColor];
    sliderBar.itemSelectedColor = kThemeColor;
    sliderBar.sliderColor = kThemeColor;
    [sliderBar slideBarItemSelectedCallback:^(NSUInteger idx) {
        [_contentView scrollSlideContentViewToIndex:idx];
    }];
    [self.view addSubview:sliderBar];
    self.slideBar = sliderBar;
    
    
    [self acquireData];
}

- (void)createContentView
{
    
    FDSlideContentView *contentView = [[FDSlideContentView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.slideBar.frame), CGRectGetWidth(self.view.frame), CGRectGetMaxY(self.view.frame) - CGRectGetMaxY(self.slideBar.frame))];
    [contentView slideContentViewScrollFinished:^(NSUInteger index) {
        // Relate to the slide bar
        [_slideBar selectSlideBarItemAtIndex:index];
    }];
    contentView.dataSource = self;
    contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentView];
    _contentView = contentView;
}

- (NSInteger)numOfContentView {
    return [self.slideBar.itemsTitle count];
}

- (UIViewController *)slideContentView:(FDSlideContentView *)contentView viewControllerForIndex:(NSUInteger)index {
    MHCategoryDetailViewController *categoryDetailVC = [[MHCategoryDetailViewController alloc] initWithCollectionViewLayout:[[UICollectionViewFlowLayout alloc]init]];
    categoryDetailVC.CatalogID = [self.categoryArray[index] CatalogID];
    __weak MHCategoryMainViewController *test = self;
    categoryDetailVC.pushComicDetailVCBlock = ^(MHComicIntroduct *comicIntroduct)
    {
        MHComicDetailViewController *comicDetailVC = [[MHComicDetailViewController alloc]init];
        comicDetailVC.comicIntroduct = comicIntroduct;
        [test.navigationController pushViewController:comicDetailVC animated:YES];
    };
    
    return categoryDetailVC;
}
@end
